package base.dao;

import base.bean.SuccessKilled;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by 30509 on 2018/3/25.
 */
@Repository
public interface SuccessKilledDao extends JpaRepository<SuccessKilled,Integer>{
    /**
     * 插入购买明细，可过滤重复
     * @param seckillId
     * @param userPhone
     * @return 插入的行数
     */
    @Modifying
    @Transactional
    @Query(value = "insert ignore into success_killed (seckill_id,user_phone) values (?1, ?2)",nativeQuery = true)
    int insertSuccessKilled(long seckillId,long userPhone);

    /**
     * 根据Id查询SuccessKilled并携带秒杀产品对象实体
     * @param seckillId
     * @return
     */
    @Query(value = "select  " +
            "sk.seckill_id, " +
            "sk.user_phone, " +
            "sk.create_time, " +
            "sk.state, " +
            "s.seckill_id," +
            "s.name, " +
            "s.number, " +
            "s.start_time, " +
            "s.end_time, " +
            "s.create_time" +
            " from success_killed  sk inner join seckill  s on sk.seckill_id = s.seckill_id " +
            "where sk.seckill_id = ?1 and sk.user_phone = ?2",nativeQuery = true)
    SuccessKilled queryByIdWithSeckill(long seckillId, long userphone);
}
